-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
HHH-18610 - Native query single table inheritance does not set/discover column positions for reading result set #9009
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…table inheritance
| for ( int k = 0; k < subclasses.size(); k++ ) { | ||
| Subclass subclass = subclasses.get( k ); | ||
| subclassClosure[k] = subclass.getEntityName(); | ||
| subclassClosure[k+1] = subclass.getEntityName(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This off by one error was accidentally introduced in commit a7da407 during a refactor from a foreach loop to a for loop.
|
|
||
| // SUBCLASSES | ||
| final List<Subclass> subclasses = persistentClass.getSubclasses(); | ||
| final List<Subclass> subclasses = persistentClass.getSubclasses().stream().sorted(Comparator.comparing(PersistentClass::getEntityName)).collect(Collectors.toList()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The subclasses need to be ordered by the entity name to match the TreeMap with entity name keys used for AbstractEntityPersister.subclassMappingTypes. Otherwise the column orders can get switched up incorrectly and return the wrong values.
| final JdbcMapping jdbcMapping = (selectable instanceof DiscriminatorMapping) ? | ||
| ((DiscriminatorMapping)selectable).getUnderlyingJdbcMapping() : | ||
| selectable.getJdbcMapping(); | ||
| return createColumnReferenceKey( tableReference, selectable.getSelectablePath(), jdbcMapping ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixes the discriminator case by using the underlying JDBC type as part of the key to look up the discriminator property in the sqlSelectionMap in DomainResultCreationStateImpl.resolveSqlExpression(ColumnReferenceKey key, Function<SqlAstProcessingState, Expression> creator) function.
|
Closing since this was fixed via #9291 already. |
When using a native query on an entity with single table inheritance, hibernate does not set/discover the column positions for reading the result set. This is a regression from 5.6.15 to 6.x.
I've attached a test case that demonstrates the issue. Note that it may appear to work using a database with a lenient JDBC driver (H2/HSQL) because it will allow you to specify the table column name instead of the alias when retrieving results. However with a strict JDBC driver (such as PostgreSQL) it will fail since only the alias names are available in the JDBC metadata. Even though the lenient driver appears to work, it is incorrect when a property on two subclasses have the same name but different column names.
This first commit fixes the issue for the discriminator column name, but there is a similar issue with the subclass column names not working that I was not able to address. The main issue with fixing that is that it appears that we need to add those subclass properties to the
sqlSelectionMapinDomainResultCreationStateImplvia theResultSetMappingProcessor.createSuffixedResultBuilder(EntityMappingType, String, String,LockMode, NavigablePath)method like we do with the base class property names (ResultSetMappingProcessor.java line 325). However I'm not quite sure how to do this because of the fact that that map uses aColumnReferenceKeyas a key which only consists of thetableQualifier,selectablePath, andjdbcMapping. I don’t think that will work for subclasses that have properties with the same name but different column names.Because of this remaining issue this PR is not complete and the
itShouldGetPersonsanditShouldGetWifetests do not pass yet with thematrix_pg(and probably other databases).By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license
and can be relicensed under the terms of the LGPL v2.1 license in the future at the maintainers' discretion.
For more information on licensing, please check here.
https://hibernate.atlassian.net/browse/HHH-18610